package com.hrpmar.controller;

import com.hrpmar.controller.dto.MedicperDto;
import com.hrpmar.pojo.*;
import com.hrpmar.service.MedicperService;
import com.hrpmar.utils.Constants;
import com.hrpmar.utils.SystemContext;
import com.hrpmar.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by 李松松 on 2017/11/17.
 */
@Controller
@RequestMapping("/doc")
public class MedicperController {


    @Resource
    private MedicperService medicperService;

    @PostMapping("/add")
    public String add(Worker worker, Medicper medicper,Model model){
            worker.setLandCode(Tools.string2pinyin(worker.getWoName()));
            Integer i = medicperService.addWorkerAndMedicperAspect(worker,medicper);
            String result = (i>0?"操作成功！":"操作失败！");
        System.out.println("操作结果:"+result);
            model.addAttribute("result",result);
        return "doc/view";
    }

    @GetMapping("/add")
    public String add(Model model){
        List<Role> list = medicperService.getRoleByWoJubnum(2);//获取角色List
        String woJubnum = medicperService.searchDescWorker();//获取员工编号
        HttpSession session = SystemContext.getHtppSession();//获取Session
        Worker worker = (Worker) session.getAttribute(Constants.LOGIN_USER);//获取Session对象
        Department dep = medicperService.searchByroleId(worker.getRoleId());//获取部门信息
        if(dep.getDeId()==1&&dep.getDeName().equals("医院管理")){
            model.addAttribute("bool","bool");
        }else{
            Division d = medicperService.searchDivisionBywoJubnum(worker.getWoJubnum());
            model.addAttribute("division",d);
        }
        model.addAttribute("role",list);
        model.addAttribute("woJubnum",woJubnum);
        return "doc/add";
    }
    @GetMapping("edit")
    public String searchMedicperGet(String woJubnum, Model model){
        MedicperDto dto = search();
        if(dto.getDep().getDeId()==1&&dto.getDep().getDeName().equals("医院管理")){
            List<Division> list = medicperService.getDivision();
            model.addAttribute("list",list);
        }
        List<Role> roleList = medicperService.getRoleByWoJubnum(2);

        Medicper medicper = medicperService.searchmedicperX(woJubnum);//查询修改的信息
        model.addAttribute("medicper",medicper);
        model.addAttribute("roleList",roleList);
        return "doc/edit";
    }

    @PostMapping("update")
    public String update(Worker worker, Medicper medicper,Model model){
        System.out.println(worker.toString());
        System.out.println(medicper.toString());
        Integer i = medicperService.updateMedicperAspect(medicper,worker);
        String msg = (i>0?"操作成功!":"操作失败!");
        System.out.println(msg);
        model.addAttribute("result",msg);
        return "doc/view";
    }

    @GetMapping("/display")
    public String searchDisplayInfo(String woJubnum, Model model,Integer perId){
        Worker worker = medicperService.searchDisplayInfo(woJubnum);
        model.addAttribute("worker",worker);
        model.addAttribute("perId",perId);
        return "doc/display";
    }
    public MedicperDto search(){
        MedicperDto dto = new MedicperDto();
        HttpSession session = SystemContext.getHtppSession();//获取Session
        Worker worker = (Worker) session.getAttribute(Constants.LOGIN_USER);//获取Session对象
        Department dep = medicperService.searchByroleId(worker.getRoleId());
        dto.setWoJubnum(worker.getWoJubnum());
        dto.getDep().setDeId(dep.getDeId());
        dto.getDep().setDeName(dep.getDeName());
        return dto;
    }
    //上一个
    @GetMapping("top")
    public String top(Integer perId,Model model){
        MedicperDto dto = search();
        Worker workerx = null;
        try {
            workerx = medicperService.topWorkerOne(dto,perId);
        }catch (Exception ex){

        }
        if(workerx==null){
            Medicper medicper2 = medicperService.searchNextPerId(dto,perId);
            workerx = medicperService.topWorkerOne(dto,medicper2.getPerId());
        }
        Integer perIds = null;
        Medicper medicper = medicperService.searchTopPerId(dto,perId);
        if(medicper!=null){
            perIds = medicper.getPerId();
        } else{
            perIds = perId;
        }
        model.addAttribute("worker",workerx);
        model.addAttribute("perId",perIds);
        return "doc/display";
    }
    //下一个
    @GetMapping("next")
    public String next(Integer perId,Model model){
        MedicperDto dto = search();
        Worker workerx = null;
        workerx = medicperService.NextWorkerOne(dto,perId);
        if(workerx==null){
            Medicper medicper2 = medicperService.searchTopPerId(dto,perId);
            workerx = medicperService.topWorkerOne(dto,medicper2.getPerId());
        }
        Integer perIds = null;
        Medicper medicper = medicperService.searchNextPerId(dto,perId);
        if(medicper!=null){
            perIds = medicper.getPerId();
        } else{
            perIds = perId;
        }
        model.addAttribute("worker",workerx);
        model.addAttribute("perId",perIds);
        return "doc/display";
    }
}
